﻿using AutoMapper;
using NetTaste;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VM.Common.Result;
using VM.Entity.EntityDto.System.Code;
using VM.Entity.EntityMap.System.Code;
using VM.Entity.EntityMap.Topic;
using VM.IDAL.System.Code;

namespace VM.DAL.System.Code
{
    public class codeHelperDAL : BaseDAL, ICodeHelperDAL
    {

        protected ISqlSugarClient _Client { get; set; }
        protected IMapper _IMapper { get; set; }

        public codeHelperDAL(ISqlSugarClient client, IMapper iMapper) : base(client)
        {
            _Client = client;
            _IMapper = iMapper;
        }


        public int addCodeRule(sys_codeRule obj)
        {
         int Res =   _Client.Insertable<sys_codeRule>(obj).ExecuteCommand();
         return Res;

        }

        public int deleteCodeRule(sys_codeRule obj)
        {
            throw new NotImplementedException();
        }

        public PagingData<getCodeRuleListDto> getCodeRuleListDto(string Name, int UserId, int pageSize, int pageIndex)
        {

            var listRes = _Client.Queryable<sys_codeRule>()
                    .Includes(a => a.createUser)
                    .Includes(a => a.updateUser)
                    .WhereIF(Name != "" || Name != null, a => a.name.Contains(Name))
                    .Where (a=>a.billStatus!=999)
                    ;

            PagingData<sys_codeRule> result = new PagingData<sys_codeRule>()
            {
                dataList = listRes.ToPageList(pageIndex, pageSize),
                pageIndex = pageIndex,
                pageSize = pageSize,
                recordCount = listRes.Count(),
            };

            PagingData<getCodeRuleListDto> resend = _IMapper.Map<PagingData<sys_codeRule>, PagingData<getCodeRuleListDto>>(result);
            return resend;
             
        }

        public int updateCodeRule(sys_codeRule obj)
        {
         int Res=   _Client.Updateable<sys_codeRule>(obj)
                .IgnoreColumns(ignoreAllNullColumns: true)
                .ExecuteCommand();

            return Res;
        }


    }
}
